﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class HoatDongKHKT
    {
        public String MaHoatDongKHKT;
        public String MaQuaTrinhHoatDongKHKT;
        public String MaHoiNghiKHKT;
        public String MaKhenThuong;

        public List<QuaTrinhHoatDongKHKT> DSQuaTrinhHoatDongKHKT;
        public List<HoiNghiKHKT> DSHoiNghiKHKT;
        public List<KhenThuong> DSKhenThuong;

        public List<KhenThuong> getListKhenTHuong()
        {

            return KhenThuong.getAllKhenThuong(MaKhenThuong);
        }

        public List<HoiNghiKHKT> getDSHoiNghiKHKT()
        {
            return HoiNghiKHKT.getAllHoiNghiKHKT(MaHoiNghiKHKT);
        }
        public List<QuaTrinhHoatDongKHKT> getDSQuaTrinhHDKHKT()
        {
            return QuaTrinhHoatDongKHKT.getAllQuaTrinhHoatDongKHKT(MaQuaTrinhHoatDongKHKT);
        }
        public HoatDongKHKT(String _maHoatDongKHKT)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM HoatDongKHKT " +
                "WHERE MaHoatDongKHKT = '" + _maHoatDongKHKT + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                MaHoatDongKHKT = _maHoatDongKHKT;
                MaQuaTrinhHoatDongKHKT = row["MaQuaTrinhHDKHKT"].ToString();
                MaHoiNghiKHKT = row["MaHoiNghiKHKT"].ToString();
                MaKhenThuong = row["MaKhenThuong"].ToString();
            }
            else
                MessageBox.Show(result.Error);
        }

        public HoatDongKHKT(String _maQuaTrinhHDKHKT, String _maHoiNghiKHKT, String _maKhenThuong)
        {
            MaHoatDongKHKT = Database.getID("HoatDongKHKT", "MaHoatDongKHKT");
            MaQuaTrinhHoatDongKHKT = _maQuaTrinhHDKHKT;
            MaHoiNghiKHKT = _maHoiNghiKHKT;
            MaKhenThuong = _maKhenThuong;
        }

        public Boolean Save()
        {
            QueryResult result;

            //Insert
            result = Database.shareDatabase.Query("INSERT INTO HoatDongKHKT VALUES(" +
                "'" + MaHoatDongKHKT + "', " +
                "'" + MaQuaTrinhHoatDongKHKT + "', " +
                "'" + MaHoiNghiKHKT + "', " +
                "'" + MaKhenThuong + "')");
            if (result.ReturnCode == 200)
            {
                // Không save các object con
                return true;
            }
            else
                return false;
        }
        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM HoatDongKHKT " +
                "WHERE MaHoatDongKHKT = '" + MaHoatDongKHKT + "'");
            if (result.ReturnCode == 200)
            {
                foreach (QuaTrinhHoatDongKHKT qthdkhkt in DSQuaTrinhHoatDongKHKT)
                    qthdkhkt.Delete();
                foreach (HoiNghiKHKT hnkhkt in DSHoiNghiKHKT)
                    hnkhkt.Delete();
                foreach (KhenThuong kt in DSKhenThuong)
                    kt.Delete();
                return true;
            }
            else
                return false;
        }
    }
}
